﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Efcore.MultiTenant
{
    /// <summary>
    /// 查找数据库连接KEY：
    /// 默认：Other为空：TenantId ;Other不为空：TenantId_Other ;
    /// </summary>
    public class TenantInfo
    {
        /// <summary>
        /// 租户唯一识别号
        /// 分表多租户时，为表前缀,格式：TenantId_表名
        /// </summary>
        public string TenantId { get; set; }

        /// <summary>
        /// 其他扩展
        /// </summary>
        public string Other { get; set; }

        /// <summary>
        /// 自定义dbkey函数
        /// 默认：Other为空：TenantId ;Other不为空：TenantId_Other ;
        /// </summary>
        public Func<string> DbKeyAction { get; set; }

        /// <summary>
        /// 获取dbkey
        /// </summary>
        /// <returns></returns>
        public string GetDbKey()
        {
            var userDbKey = DbKeyAction?.Invoke();
            return string.IsNullOrWhiteSpace(userDbKey) ? (string.IsNullOrWhiteSpace(Other) ? TenantId : $"{TenantId}_{Other}") : userDbKey;
        }
    }
}
